import requests


class Data:

    headers = {
        "Host": "api.bilibili.com",
        "Referer": "https://www.bilibili.com",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    }

    def get_user_info(self, mid):
        """获取用户信息

        Args:
            mid (str): 用户mid号

        Returns:
            json: user_info.json
        """
        user_info = requests.get(
            url="https://api.bilibili.com/x/space/acc/info?mid=" + mid,
            headers=self.headers,
        ).json()
        user_total = requests.get(
            "https://api.bilibili.com/x/relation/followers?vmid=" + mid + "&pn=1&ps=1",
            headers=self.headers,
        ).json()["data"]["total"]
        user_info["data"]["total"] = user_total
        return user_info

    def get_video_list(self, mid):
        """获取用户视频列表

        Args:
            mid (str): 用户mid号

        Returns:
            json: video_list.json
        """
        pn = 1
        video_list_data = requests.get(
            url="https://api.bilibili.com/x/space/arc/search?mid="
            + mid
            + "&ps=30&pn=1",
            headers=self.headers,
        ).json()

        video_list_data_all = video_list_data["data"]["list"]["vlist"]
        while (
            int(video_list_data["data"]["page"]["count"])
            > int(video_list_data["data"]["page"]["pn"]) * 30
        ):
            pn += 1
            video_list_data = requests.get(
                url="https://api.bilibili.com/x/space/arc/search?mid="
                + mid
                + "&ps=30&pn=" + str(pn),
                headers=self.headers,
            ).json()
            video_list_data_all += video_list_data["data"]["list"]["vlist"]

        return video_list_data_all

    def get_video_info(self, bv):
        """获取视频信息

        Args:
            bv (str): bv号

        Returns:
            json: video_info.json
        """
        return requests.get(
            url="https://api.bilibili.com/x/web-interface/view?bvid=" + bv,
            headers=self.headers,
        ).json()


if __name__ == "__main__":
    data = Data()
    user_total = requests.get(
            "https://api.bilibili.com/x/relation/followers?vmid=" + str(4131105) + "&pn=1&ps=1",
            headers=data.headers,
        ).json()["data"]["total"]
    print(user_total)
